package getsmart.project.database;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class LogActivity {
	private static final String ID_LOG = "ID_Log";
	private static final String ID_PROFIL = "ID_Profil";
	private static final String WAKTU = "Waktu";
	private static final String KEGIATAN = "Kegiatan";
	 
	private static final String NAMA_DB = "LogDB";// nama database
	private static final String NAMA_TABEL = "Log";// nama tabel
	private static final int DB_VERSION = 1;
 
	private static final String CREATE_TABLE_LOG = "create table "+NAMA_TABEL+" ( "+ID_LOG+" integer PRIMARY KEY autoincrement,"+ID_PROFIL+" integer, "+WAKTU+" text,"+KEGIATAN+" text)";
	private final Context context;
	private DatabaseOpenHelper dbHelper;
	private SQLiteDatabase db;
 
	public LogActivity(Context ctx) {
		this.context = ctx;
		dbHelper = new DatabaseOpenHelper(context);
		db = dbHelper.getWritableDatabase();
	}
 
	private static class DatabaseOpenHelper extends SQLiteOpenHelper {
		 
		public DatabaseOpenHelper(Context context) {
			super(context, NAMA_DB, null, DB_VERSION);
			// TODO Auto-generated constructor stub
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			// TODO Auto-generated method stub
			db.execSQL(CREATE_TABLE_LOG);
		}
 
		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVer, int newVer) {
			// TODO Auto-generated method stub
			db.execSQL("DROP TABLE IF EXISTS "+NAMA_TABEL);
			onCreate(db);
 
		}
	}
 
	public void close() {
		dbHelper.close();
	}
	
	public void addLog(int idProfil, String Kegiatan){
		ContentValues values = new ContentValues();
		SimpleDateFormat sdf = new SimpleDateFormat("ddMMyyyy_HHmmss");
		String currentDateandTime = sdf.format(new Date());
		values.put(ID_PROFIL, idProfil);
		values.put(WAKTU, currentDateandTime);
		values.put(KEGIATAN, Kegiatan);
		
		try {
				db.insert(NAMA_TABEL, null, values);
			} catch (Exception e) {
				Log.e("DB ERROR", e.toString());
				e.printStackTrace();
			}
	}
	
	public ArrayList<ArrayList<Object>> getLog(){
		ArrayList<ArrayList<Object>> dataArray = new ArrayList<ArrayList<Object>>();
		Cursor cur;
		try {
			cur = db.query(NAMA_TABEL, new String[] {ID_LOG, ID_PROFIL, WAKTU, KEGIATAN}, null, null, null, null, null);
			cur.moveToFirst();
			if (!cur.isAfterLast()) {
				do {
					ArrayList<Object> dataList = new ArrayList<Object>();
					dataList.add(cur.getLong(0));
					dataList.add(cur.getLong(1));
					dataList.add(cur.getString(2));
					dataList.add(cur.getString(3));
					dataArray.add(dataList);
 
				} while (cur.moveToNext());
 
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Log.e("DEBE ERROR", e.toString());
		}
		return dataArray;
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
	}
}
